﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ulitity;

namespace School.WebPages.Download
{
    public partial class DownloadExecute : System.Web.UI.Page
    {
        TechnologySchoolEntities db = new TechnologySchoolEntities();

        protected void Page_Load(object sender, EventArgs e)
        {
            string filePath = Request.QueryString["FilePath"];
            string saveFileName = filePath.Substring(filePath.LastIndexOf("\\") + 1);
            string sourceFilePath = Server.MapPath(filePath);
            if (System.IO.File.Exists(sourceFilePath))
            {
                Page.Response.Clear();
                bool success = Ulitity.FileDownloadHelper.ResponseFile(Page.Request, Page.Response, saveFileName, sourceFilePath, 1024000);
                if (!success)
                {
                    //MessageBox.Show("下载文件出错！", DotNetSources.Web.UI.Buttons.Popup.Icon.Error);
                    Page.Response.End();
                }
                else
                {
                    int fid = int.Parse(Request.QueryString["FID"]);
                    var file = db.DownloadFile.FirstOrDefault(d => d.FID == fid);
                    file.Hits += 1;
                    db.SaveChanges();
                }
            }
            else
            {
                Response.Write("<span class='error'>对不起，当前下载的文档不存在，请与管理员联系！</span>");
            }
        }
    }
}
